package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.module.Tenant;
import com.sixmac.module.TenantLive;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by admin on 2018/9/5.
 */
@Controller
@RequestMapping("/live")
@ResponseBody
public class LiveController extends BaseController {


    /**
     * @api {post} /live/doApply 0.1 提交申请
     * @apiName live.doApply
     * @apiGroup live
     * @apiParam {String} token token
     */
    @AuthUser
    @RequestMapping("/doApply")
    public AjaxResult liveApply(Tenant tenant) {

        TenantLive tenantLive = Blade.create(TenantLive.class).findFirstBy("status=1 and tenantId="+tenant.getId(),null);

        if (tenantLive!=null){
            return error("居住证申请审核中，请勿重复申请。");
        }

        tenantLive = new TenantLive();
        tenantLive.setStatus(1);
        tenantLive.setCreateDate(new Date());
        tenantLive.setTenantId(tenant.getId());
        boolean result = Blade.create(TenantLive.class).save(tenantLive);

        if (result)
            return success("居住证申请已提交，请等待审核。");

        return error("居住证申请提交失败，请联系房东。");
    }
}
